//
//  CommentToolbar.m
//  ArtDog
//
//  Created by dllo on 16/9/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommentToolbar.h"
#import "UIColor+Common.h"
#import "Masonry.h"
#import "NSString+Common.h"
#import "HLMacro.h"
#import "MineMainViewController.h"
#import "UIView+Common.h"
#import "ReminderView.h"

@interface CommentToolbar ()<UITextViewDelegate>

@property (nonatomic, strong) UIView * shadowView;


@property (nonatomic, strong) UILabel *placeholderLabel;

@property (nonatomic, assign) NSInteger height;

@end

@implementation CommentToolbar

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        [self layoutAllViews];
    }
    return self;
}

- (void)setupSubviews
{
    _height = 30;
    _leftImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"image"]];
    [self addSubview:_leftImageView];
    
   
    _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_sendBtn];
    [_sendBtn setTitle:@"发言" forState:UIControlStateNormal];
    [_sendBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_sendBtn addTarget:self action:@selector(sendAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _shadowView = [[UIView alloc]init];
    [self addSubview:_shadowView];
    _shadowView.backgroundColor = [UIColor colorFromHexRGB:@"e2e2e2"];
    
    _textView = [[UITextView alloc]init];
    [self addSubview:_textView];
    _textView.backgroundColor = [UIColor colorFromHexRGB:@"fafafa"];
    _textView.delegate = self;
    _textView.layer.cornerRadius = 3;
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = [UIColor colorFromHexRGB:@"cccccc"].CGColor;
    _textView.font = [UIFont systemFontOfSize:13];
    _textView.returnKeyType = UIReturnKeySend;
    
    _placeholderLabel = [[UILabel alloc]init];
    [self addSubview:_placeholderLabel];
    _placeholderLabel.textColor = [UIColor colorFromHexRGB:@"aaaaaa"];
    _placeholderLabel.text = @"评论获1积分，单日上限30";
    _placeholderLabel.font = [UIFont systemFontOfSize:13];
}

- (UIImageView *)leftImageView
{
    if (_leftImageView.hidden == YES) {
        [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.top.equalTo(self).offset(10);
            make.bottom.equalTo(self).offset(-10);
            make.height.mas_equalTo(30);
        }];
    }
    return _leftImageView;
}

- (void)layoutAllViews
{
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(15);
        make.bottom.equalTo(self).offset(-12.5);
        make.width.height.mas_equalTo(24);
    }];
    [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self).offset(-12.5);
        make.width.mas_equalTo(30);
    }];
    [_shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(_textView.mas_top).offset(-8);
    }];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_leftImageView.mas_right).offset(10);
        make.right.equalTo(_sendBtn.mas_left).offset(-10);
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
        make.height.mas_equalTo(30);
    }];
    [_placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_textView);
        make.left.equalTo(_textView).offset(10);
    }];
}

- (void)sendAction:(UIButton *)button {
    if ([[NSString getUserDefaultId] isEqualToString:@"0"]) {
        UIViewController *vc = [self getVCRecently];
        UINavigationController *mineNavi = vc.tabBarController.viewControllers[3];
        MineMainViewController *mineVC = mineNavi.viewControllers[0];
        mineVC.lastIndex = vc.tabBarController.selectedIndex;
        vc.tabBarController.selectedIndex = 3;
    } else if (_textView.text == nil || [[_textView.text stringByReplacingOccurrencesOfString:@" " withString:@""]stringByReplacingOccurrencesOfString:@"\n" withString:@""].length == 0) {
        [ReminderView reminderWithSuperView:self.superview andTitle:@"请输入评论" andPosition:HLAlertViewPositionCenter];
        return;
    } else {
        if ([self.delegate respondsToSelector:@selector(sendCommentFromeButton:andSendButton:)]) {
            [self.delegate sendCommentFromeButton:_textView andSendButton:button];
        }
        [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_leftImageView.mas_right).offset(10);
            make.right.equalTo(_sendBtn.mas_left).offset(-10);
            make.top.equalTo(self).offset(10);
            make.bottom.equalTo(self).offset(-10);
            make.height.mas_equalTo(30);
        }];
        [ReminderView reminderWithSuperView:self.superview andTitle:@"评论发送成功！" andPosition:HLAlertViewPositionCenter];
        [_textView resignFirstResponder];
    }
    

}

- (void)textViewDidChange:(UITextView *)textView
{
    if (_textView.text.length > 0) {
        _placeholderLabel.hidden = YES;
    } else {
        _placeholderLabel.hidden = NO;
    }
    [_textView layoutIfNeeded];
    CGFloat height = [_textView.text calculateWordsHeightWithFontSize:13 andWidth:_textView.frame.size.width - 11];
    if (height != _height) {
        _height = height;
        if (height > 30 && height < 64) {
            if (_leftImageView.hidden == YES) {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(15);
                    make.right.equalTo(self).offset(-15);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(height);
                }];
            } else {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(_leftImageView.mas_right).offset(10);
                    make.right.equalTo(_sendBtn.mas_left).offset(-10);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(height);
                }];
            }
        } else if (height < 30) {
            if (_leftImageView.hidden == YES) {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(15);
                    make.right.equalTo(self).offset(-15);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(30);
                }];
            } else {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(_leftImageView.mas_right).offset(10);
                    make.right.equalTo(_sendBtn.mas_left).offset(-10);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(30);
                }];
            }

        }
    }
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([[NSString getUserDefaultId] isEqualToString:@"0"]) {
        UIViewController *vc = [self getVCRecently];
        UINavigationController *mineNavi = vc.tabBarController.viewControllers[3];
        MineMainViewController *mineVC = mineNavi.viewControllers[0];
        mineVC.lastIndex = vc.tabBarController.selectedIndex;
        vc.tabBarController.selectedIndex = 3;
        return NO;
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        if (textView.text == nil || [[textView.text stringByReplacingOccurrencesOfString:@" " withString:@""]stringByReplacingOccurrencesOfString:@"\n" withString:@""].length == 0) {
            [ReminderView reminderWithSuperView:self.superview andTitle:@"请输入评论" andPosition:HLAlertViewPositionCenter];
            return NO;
        } else {
            if ([self.delegate respondsToSelector:@selector(sendCommentFromeKeyboard:)]) {
                [self.delegate sendCommentFromeKeyboard:textView];
            }
            if (_leftImageView.hidden == YES) {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(self).offset(15);
                    make.right.equalTo(self).offset(-15);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(30);
                }];
            } else {
                [_textView mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(_leftImageView.mas_right).offset(10);
                    make.right.equalTo(_sendBtn.mas_left).offset(-10);
                    make.top.equalTo(self).offset(10);
                    make.bottom.equalTo(self).offset(-10);
                    make.height.mas_equalTo(30);
                }];
            }
            [ReminderView reminderWithSuperView:self.superview andTitle:@"评论发送成功！" andPosition:HLAlertViewPositionCenter];
            textView.text = nil;
            _placeholderLabel.hidden = NO;
            [textView resignFirstResponder];
        }
        return NO;
    }
    return YES;
}

@end
